package com.yupi.springbootinit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yupi.springbootinit.common.ErrorCode;
import com.yupi.springbootinit.constant.CommonConstant;
import com.yupi.springbootinit.constant.CourtConstant;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.court.CourtQueryRequest;
import com.yupi.springbootinit.model.dto.court.UpdataCourtRequest;
import com.yupi.springbootinit.model.dto.user.UserQueryRequest;
import com.yupi.springbootinit.model.entity.Court;
import com.yupi.springbootinit.model.entity.User;
import com.yupi.springbootinit.service.CourtService;
import com.yupi.springbootinit.mapper.CourtMapper;
import com.yupi.springbootinit.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Lenovo
* @description 针对表【court(场馆信息表)】的数据库操作Service实现
* @createDate 2024-04-20 15:29:17
*/
@Service
public class CourtServiceImpl extends ServiceImpl<CourtMapper, Court>
    implements CourtService{

    @Override
    public Boolean addCourt(String courtName, String courtProvince, String courtCity, String courtAddress, String phoneNumber, Integer courtArea, String courtMain, String businessSatus) {
        Court court=new Court();

        court.setCourtName(courtName);
        court.setCourtProvince(courtProvince);
        court.setCourtcity(courtCity);
        court.setCourtaddres(courtAddress);
        court.setPhoneNumber(phoneNumber);
        court.setCourtArea(courtArea);
        court.setCourtMain(courtMain);
        court.setBusinessStatus(CourtConstant.BusinessStatus_Open);

        return this.save(court);

    }

    @Override
    public List<Court> getAllCourt() {

        List<Court> list = this.list();


        return list;
    }

    @Override
    public boolean updataCourt(UpdataCourtRequest updataCourtRequest) {
        //bean copy
        Court court=new Court();
        BeanUtils.copyProperties(updataCourtRequest,court);

        return this.updateById(court);


    }

    @Override
    public boolean deleteCourt(Long id) {
        List<Court> allCourt = this.getAllCourt();
        boolean remove = this.removeById(id);
        return  true;


    }

    @Override
    public QueryWrapper<Court> getQueryWrapper(CourtQueryRequest courtQueryRequest) {
        if (courtQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = courtQueryRequest.getId();
        String courtName = courtQueryRequest.getCourtName();
        String courtProvince = courtQueryRequest.getCourtProvince();
        String courtCity = courtQueryRequest.getCourtCity();
        String courtAddress = courtQueryRequest.getCourtAddress();
        String phoneNumber = courtQueryRequest.getPhoneNumber();
        Integer courtArea = courtQueryRequest.getCourtArea();
        String courtMain = courtQueryRequest.getCourtMain();
        String businessSatus = courtQueryRequest.getBusinessSatus();
        String createTime = courtQueryRequest.getCreateTime();
        int current = courtQueryRequest.getCurrent();
        int pageSize = courtQueryRequest.getPageSize();
        String sortField = courtQueryRequest.getSortField();
        String sortOrder = courtQueryRequest.getSortOrder();
        //构造查询条件
        QueryWrapper<Court> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq(id != null, "id", id);
            queryWrapper.eq("isDelete",0);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;



    }
}




